<template>
  <div class="goods">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-input
        placeholder="请输入内容"
        v-model="pageinfo.query"
        class="input-with-select"
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="search"
        ></el-button>
      </el-input>
      <el-button type="primary" class="add" @click="$router.push('/add')">添加用户</el-button>

      <el-table :data="tableData" border stripe style="width: 100%">
        <el-table-column
          fixed
          type="index"
          width="40"
          label="#"
        ></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="700"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量">
        </el-table-column>
        <el-table-column prop="goods_number" label="商品数量">
        </el-table-column>
        <el-table-column prop="add_time" label="创建时间">
          <template slot-scope="scope">
            {{ scope.row.add_time | fileTime }}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="small"
              @click="edit(scope.row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="small"
              @click="del(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="pageinfo.pagenum"
        :page-sizes="[10,20, 30, 40]"
        :page-size.sync="pageinfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 添加商品 -->
    <el-dialog :title="editId?'编辑商品':'添加商品'" :visible.sync="dialogFormVisible">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="ruleForm.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="goods_price">
          <el-input v-model="ruleForm.goods_price" type="number"></el-input>
        </el-form-item>
        <el-form-item label="商品数量" prop="goods_number">
          <el-input v-model="ruleForm.goods_number" type="number"></el-input>
        </el-form-item>
        <el-form-item label="商品重量" prop="goods_weight">
          <el-input v-model="ruleForm.goods_weight" type="number"></el-input>
        </el-form-item>
        <el-form-item label="商品图片" prop="pics" v-if="editId ? false : true">
          <el-upload
            class="upload-demo"
            action="https://jsonplaceholder.typicode.com/posts/"
            list-type="picture"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('ruleForm')">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { pleaGoods, pleaRem, Goodsedit, getAdd } from "../utils/http";
import moment from "moment";
export default {
  data() {
    return {
      pageinfo: {
        query: "", //搜索
        pagenum: 1, //当前页
        pagesize: 10, //每页条数
      },
      total: 0, //总页数
      tableData: [],
      dialogFormVisible: false,
      ruleForm: {
        goods_name: "",
        goods_price: "",
        goods_number: "",
        goods_weight: "",
        pics: "",
      },
      rules: {
        goods_name: [
          { required: true, message: "商品名称不能为空", trigger: "blur" },
        ],
        goods_price: [
          { required: true, message: "商品价格不能为空", trigger: "blur" },
        ],
        goods_number: [
          { required: true, message: "商品数量不能为空", trigger: "blur" },
        ],
        goods_weight: [
          { required: true, message: "商品重量不能为空", trigger: "blur" },
        ],
      },
      editId: "", //保存修改的id
    };
  },
  created() {
    this.getgoods();
  },
  filters: {
    fileTime(value) {
      moment.locale("zh-ch");
      return moment(value).format("YYYY-MM-DD hh:mm:ss");
    },
  },
  methods: {
    getgoods() {
      pleaGoods(this.pageinfo).then((res) => {
        // console.log(res);

        if (res.meta.status == 200) {
          this.tableData = res.data.goods;
          this.total = res.data.total;
          this.$message({
            type: "success",
            message: res.meta.msg,
          });
        } else {
          this.$message({
            type: "danger",
            message: res.meta.msg,
          });
        }
      });
    },
    //分页
    handleSizeChange(val) {
      this.pageinfo.pagesize = val;
      this.getgoods();
    },
    //分页
    handleCurrentChange(val) {
      this.pageinfo.pagenum = val;
      this.getgoods();
    },
    //编辑
    edit(row) {
      this.dialogFormVisible = true;
      this.editId = row.goods_id;
      this.ruleForm = { ...row };
    },
    //删除:
    del(row) {
      console.log(row);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          pleaRem({ id: row.goods_id }).then((res) => {
            console.log(res);
            this.getgoods();
          });
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //搜索
    search() {
      this.getgoods();
    },
    //确定添加
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert("submit!");
          //添加
          let obj = {
            ...this.ruleForm,
            goods_cat: "1,2,3",
          };
          let objs = {
            ...this.ruleForm,
            id: this.ruleForm.goods_id,
            goods_cat: "1,2,3",
          };
          // console.log(this.ruleForm);
          if (this.editId) {
            //修改
            console.log(this.ruleForm);
            console.log(this.editId);
            Goodsedit(objs).then((res) => {
              console.log(res);
              if (res.meta.status == 200) {
                this.getgoods();
                this.dialogFormVisible = false;
                this.ruleForm = {};
                this.$message({
                  type: "success",
                  message: res.meta.msg,
                });
              } else {
                this.$message({
                  type: "danger",
                  message: res.meta.msg,
                });
              }
            });
          } else {
            getAdd(obj).then((res) => {
              if (res.meta.status == 201) {
                this.getgoods();
                this.dialogFormVisible = false;
                this.ruleForm = {};
                this.$message({
                  type: "success",
                  message: res.meta.msg,
                });
              } else {
                this.$message({
                  type: "danger",
                  message: res.meta.msg,
                });
              }
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dialogFormVisible = false;
    },
    //图片
    handlePreview(file, fileList) {
      console.log(file, fileList);
    },
    handleRemove(file) {
      console.log(file);
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-breadcrumb {
  margin-bottom: 20px;
}
::v-deep .input-with-select {
  width: 475px;
  margin-bottom: 20px;
}
.add {
  margin-left: 20px;
}
::v-deep .el-pagination {
  margin-top: 15px;
}
</style>